package com.fcx.workai.jinchang.ui.welcome;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.fcx.workai.jinchang.R;
import com.fcx.workai.jinchang.ui.GuideActivity;
import com.fcx.workai.jinchang.ui.main.MainActivity;
import com.fcx.workai.loginmodule.ui.LoginMainActivity;
import com.hxh.component.basicore.component.mvp.view.BaseActivity;
import com.hxh.component.basicore.util.Utils;
import com.hxh.component.basicore.util.permissions.PermissionActivityActivity;
import com.hxh.component.basicore.util.permissions.PermissionsChecker;
import com.hxh.component.business.app.App;
import com.hxh.component.business.arouter.MyAppProvider;
import com.hxh.component.business.bean.OpenProgressBean;
import com.hxh.component.business.ui.bean.UserCompanyListBean;
import com.hxh.component.business.utils.Constant;

import static com.fcx.workai.loginmodule.ui.LoginMainActivity.BUNDLE_FAIL_REASON;
import static com.fcx.workai.loginmodule.ui.LoginMainActivity.BUNDLE_VERIFY_STEP;
import static com.hxh.component.business.utils.Constant.SpTag.SPACE_NUMBER_ID;

/**
 * des:欢迎页
 *
 * @author fuchengxiang
 * @version 1.0
 * @date 2019/8/23
 */

public class WelcomeActivity extends BaseActivity<WelcomePresenter> implements WelcomeContract.V {
    // 所需的全部权限
    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.INTERNET,
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_PHONE_STATE
    };
    private PermissionsChecker mPermissionsChecker;
    private View rootView;

    @Override
    public int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    public void initData(Bundle saveInstanceState) {
        mPermissionsChecker = new PermissionsChecker(this);
    }


    @Override
    public WelcomePresenter newP() {
        return new WelcomePresenter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isTaskRoot()) {
            finish();
            return;
        }

        if (mPermissionsChecker.checkPermissions(PERMISSIONS)) {
            PermissionActivityActivity.startActivityForResult(this, 0, PERMISSIONS);
        } else {
            getView().postDelayed(new Runnable() {
                @Override
                public void run() {
                    boolean isFirstStart = Utils.SP.getBoolean(Constant.SpTag.IS_FIRST_START, true);
                    boolean isnouser = App.getUserDTO().getUser() != null ? false : true;
                    if (isFirstStart) {
                        startActivity(new Intent(WelcomeActivity.this, GuideActivity.class));
                        mPermissionsChecker = null;
                        WelcomeActivity.this.finish();
                    } else {
                        if (isnouser) {
                            startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                            WelcomeActivity.this.finish();
                        } else {
                            MyAppProvider.startLoginActivty(Constant.RouterTag.SHOWTAG, Constant.RouterTag.LOGIN_SHOWTYPE_SMSLOGIN);
                            WelcomeActivity.this.finish();
                        }
                    }
                }
            }, 1500);
        }

    }

    /**
     * 跳转至登录相关页
     *
     * @param routerTag
     */
    private void goRouterActivty(String routerTag) {
        Intent intent = new Intent(this, LoginMainActivity.class);
        intent.putExtra(Constant.RouterTag.SHOWTAG, routerTag);
        startActivity(intent);
    }

    /**
     * 跳转至验证步骤相关页
     *
     * @param routerTag
     * @param step
     */
    private void goRouterActivty(String routerTag, int step) {
        Intent intent = new Intent(this, LoginMainActivity.class);
        intent.putExtra(Constant.RouterTag.SHOWTAG, routerTag);
        intent.putExtra(BUNDLE_VERIFY_STEP, step);
        startActivity(intent);
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 拒绝时, 关闭页面, 缺少主要权限, 无法运行
        if (requestCode == 0 && resultCode == PermissionActivityActivity.PERMISSIONS_DENIED) {
            finish();
        } else {

        }
    }
}
